#!/usr/bin/env python3
# _*_coding:utf-8 _*_
__author__ = "Gao"
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
from django.http import HttpResponse
from django.http import JsonResponse

class AllowCrossDomain(MiddlewareMixin):

    def process_response(self, request, response):
        response["Access-Control-Allow-Origin"] = "*"
        response["Access-Control-Allow-Methods"] = "GET,POST,DELETE,PUT"
        response["Access-Control-Max-Age"] = "1728000"
        response["Access-Control-Allow-Headers"] =  "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,Authorization,X-Requested-With"
        # response["Access-Control-Allow-Headers"] =  "*"
        return response


class HideServerError(MiddlewareMixin):
    """隐藏500错误详细信息"""
    def process_response(self, request, response):
        if response.status_code == 500:
            return JsonResponse({'non_field_errors':['系统繁忙']}, status=500)
        return response

